package com.kevin.controller.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kevin.common.api.PageDTO;
import com.kevin.common.api.R;
import com.kevin.service.config.StpUserHolder;
import com.kevin.service.entity.SysMenu;
import com.kevin.service.entity.SysRole;
import com.kevin.service.entity.SysUser;
import com.kevin.service.mapper.SysUserMapper;
import com.kevin.service.query.SysRoleUserQuery;
import com.kevin.service.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 系统用户控制器
 *
 * @author wh
 * @date 2023/03/27
 */
@RestController
@RequestMapping("sys/role")
@AllArgsConstructor
@Api(tags = "角色管理")
public class SysRoleController {
    private final SysRoleService roleService;
    private final SysUserService sysUserService;
    private final SysUserMapper userMapper;
    private final SysRoleMenuService sysRoleMenuService;
    private final SysMenuService sysMenuService;
    private final SysUserRoleService sysUserRoleService;

    @GetMapping("page")
    @ApiOperation(value = "分页")
    @SaCheckPermission(value = "sys:role:page")
    public R<PageInfo<SysRole>> page(PageDTO pageDTO, String name) {
        PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
        return R.ok(new PageInfo<>(roleService.list(Wrappers.lambdaQuery(SysRole.class)
                .like(StrUtil.isNotBlank(name), SysRole::getName, name)
        )));
    }

    @GetMapping("list")
    @ApiOperation(value = "列表")
    @SaCheckPermission(value = "sys:role:list")
    public R<List<SysRole>> list() {
        return R.ok(roleService.list());
    }

    @GetMapping("{id}")
    @ApiOperation(value = "信息")
    @SaCheckPermission(value = "sys:role:info")
    public R<SysRole> get(@PathVariable("id") Long id) {
        SysRole role = roleService.getById(id);

        ThreadUtil.sleep(1000);

        // 查询角色对应的菜单
        List<Long> menuIdList = sysRoleMenuService.getMenuIdList(id);
        role.setMenuIdList(menuIdList);

        return R.ok(role);
    }

    @PostMapping
    @ApiOperation(value = "保存")
    @SaCheckPermission(value = "sys:role:save")
    public R<String> save(@RequestBody @Valid SysRole vo) {
        roleService.saveRole(vo);
        return R.ok();
    }

    @PutMapping
    @ApiOperation(value = "修改")
    @SaCheckPermission(value = "sys:role:update")
    public R<String> update(@RequestBody @Valid SysRole vo) {
        roleService.updateRole(vo);
        return R.ok();
    }

    @DeleteMapping
    @ApiOperation(value = "删除")
    @SaCheckPermission(value = "sys:role:delete")
    public R<String> delete(@RequestBody List<Long> idList) {
        roleService.removeRole(idList);
        return R.ok();
    }

    @GetMapping("menu")
    @ApiOperation(value = "角色菜单")
    @SaCheckPermission(value = "sys:role:menu")
    public R<List<SysMenu>> menu() {
        SysUser user = StpUserHolder.getUserFromSession();
        return R.ok(sysMenuService.getUserMenuList(user.getId(),user.getSuperAdmin(), null));
    }

    @GetMapping("user/page")
    @ApiOperation(value = "角色用户-分页")
    @SaCheckPermission(value = "sys:role:update")
    public R<PageInfo<SysUser>> userPage(PageDTO pageDTO, @Valid SysRoleUserQuery query) {
        PageHelper.startPage(pageDTO.getPageNum(),pageDTO.getPageSize());
        return R.ok(new PageInfo<>(userMapper.getRoleUserList(query)));
    }

    @DeleteMapping("user/{roleId}")
    @ApiOperation(value = "删除角色用户")
    @SaCheckPermission(value = "sys:role:update")
    public R<String> userDelete(@PathVariable("roleId") Long roleId, @RequestBody List<Long> userIdList) {
        sysUserRoleService.deleteByUserIdList(roleId, userIdList);

        return R.ok();
    }

    @PostMapping("user/{roleId}")
    @ApiOperation(value = "分配角色给用户列表")
    @SaCheckPermission(value = "sys:role:update")
    public R<String> userSave(@PathVariable("roleId") Long roleId, @RequestBody List<Long> userIdList) {
        sysUserRoleService.saveUserList(roleId, userIdList);

        return R.ok();
    }

}
